package com.square.service.auth.provider.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.square.service.auth.provider.service.LoginService;
import com.square.service.common.consts.GlobalPrefix;
import com.square.service.common.entity.auth.vo.LoginParam;
import com.square.service.common.entity.member.po.User;
import com.square.service.common.utils.JwtUtils;
import com.square.service.member.provider.feign.UserFeign;
import com.square.service.redis.consumer.feign.RedisFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 登录认证服务层实现
 *
 * @author : 王苏
 * @since : 2021/4/19 15:47
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private RedisFeign redisFeign;
    @Resource
    private UserFeign userFeign;

    /**
     * 获取验证码
     *
     * @return
     */
    @Override
    public R<?> getvalidCode() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        String validKey = IdUtil.fastSimpleUUID();
        String redisKey = GlobalPrefix.PREFIX_LOGIN_VALIDCODE + validKey;
        R<?> r = redisFeign.set(redisKey, captcha.getCode(), 60);
        if (!r.ok()) {
            return r;
        }
        LoginParam loginParam = new LoginParam()
                .setValidCodeBase64(captcha.getImageBase64())
                .setValidKey(validKey);
        return R.ok(loginParam);
    }

    /**
     * 登录
     *
     * @param loginParam
     * @return
     */
    @Override
    public R<?> login(LoginParam loginParam) {
        R<?> r = redisFeign.get(GlobalPrefix.PREFIX_LOGIN_VALIDCODE + loginParam.getValidKey());
        if (!r.ok()) {
            return r;
        }
        String redisValidCode = r.getData().toString();
        if (StringUtils.isBlank(redisValidCode)) {
            return R.failed("验证码已失效");
        }
        if (!redisValidCode.equalsIgnoreCase(loginParam.getValidCode())) {
            return R.failed("验证码错误");
        }
        String password = SecureUtil.md5(loginParam.getPassword());
        r = userFeign.getUserByUsername(loginParam.getUsername());
        if (!r.ok()) {
            return r;
        }
        if (r.getData() == null) {
            return R.failed("无此用户");
        }
        User user = JSONObject.parseObject(JSON.toJSONString(r.getData()), User.class);
        if (!password.equals(user.getPassword())) {
            return R.failed("密码不正确");
        }
        String jwtKey = user.getUsername() + GlobalPrefix.PREFIX_SPLIT + IdUtil.fastSimpleUUID();
        String token = JwtUtils.createJwt(jwtKey, user.getPassword());
        user.setToken(token);
        redisFeign.set(GlobalPrefix.PREFIX_LOGIN_TOKEN + jwtKey, JSON.toJSONString(user), JwtUtils.JWT_EXPIRE_TIME);
        return R.ok(token);
    }
}
